import axios from 'axios'
var utils = {
  // 注册板块接口，接口文档地址：http://192.168.50.229:8080/lifetime/swagger/index.html#，看后台变动
  GET_USERTEL: '/lifetime/user/get_tel', //GET 判断手机是否已注册，检查传0
  GET_USERSMS: '/lifetime/user/sms', //POST 获取验证码
  SET_PASSWORD: '/lifetime/user/retrieve_password', //POST 找回密码
  CHENK_USER_NAME: '/lifetime/user/check_user_name', //GET 判断用户昵称是否已使用
  SGIN_USER: '/lifetime/user/login', //POST 用户登录
  SET_USET_MESSAGE: '/lifetime/user/set_user', //POST 完善个人资料
  UP_IMAGE: '/lifetime/platform/upload_image', //POST 上传图片
  GET_USER_MESSAGE: '/lifetime/user/get_user_info', //GET 获取用户信息
  USER_REPLACE_TEL:'/lifetime/user/replace_tel', //POST 更换手机号（旧手机传type3，新手机传type0;codesms新手机验证码，tel新手机）

  GET_SCHOOL: '/lifetime/mechanism/get_mechanism', // id:机构id    获取机构信息
  GET_LIST: '/lifetime/mechanism_live/get_live_list', // mechanismId:机构id  type: 0,1,2排序   获取视频列表
  GET_DETAIL: '/lifetime/mechanism_live/get_live_course', //?(回放传liveOrderId,直播传id)    获取视频详情
  ADD_COMMENT: '/lifetime/conmment/add_conmment', // commentContent: 评论内容, id:视频liveOrderId,type:11     发送评论
  GET_COMMENT: '/lifetime/conmment/get_conmment_list', // id:视频liveOrderId ,type:11,page:分页     获取视频评论
  QIUNIU_UPLOAD_URL: 'http://upload-z2.qiniup.com', // 七牛服务器地址

  GET_VIDEO_LIST: '/lifetime/user/get_video_list', //GET 获取单一视频 传参：type：系列视频传1，单一视频传0，page：1
  GET_QINIU_TOKEN: '/lifetime/video/get_token', //GET 获取七牛云上传视频token
  GET_TEACHER_MESSAGE: '/lifetime/teacher/get_teacher', //获取老师信息
  GET_USER_APPROVE: '/lifetime/teacher/get_teacher_user_id_card', // GET 获取认证信息 0是审核中或者未审核  1是审核已通过 2是审核未通过（如果是0 imageUrl为空或者长度为0未认证，imageUrl不为空待认证）
  SET_COURE_VIDEO: '/lifetime/video/set_course_video', //POST 编辑视频
  ADD_COURE_VIDEO: '/lifetime/video/add_course_video', //POST 上传视频
  DEL_COURE_VIDEO: '/lifetime/video/del_course_video', //POST 删除视频
  GET_TYPE_LIST: '/lifetime/platform/get_label_type_list', //GET 获取类目
  GET_VIDEO_DETAILS:'/lifetime/video/get_course_video',//GET 获取视频详情 id
  GET_APPROVE_INFORMATION:'/lifetime/teacher/get_teacher_user_id_card',//GET  获取认证资料
  TEACHER_APPROVE:'/lifetime/teacher/SET_user_id_card',//POST  教师认证
                

  prevent: true, //防止多次点击
  /**
   * 统一post请求
   * @param {string} url 请求路径
   * @param {*} data 数据
   * @param {funciton} callback 成功回调
   */
  $post(url, callback, data) {
    this.prevent = true
    if (!this.prevent) {
      return
    }
    this.prevent = false
    axios({
      method: 'POST',
      url: url,
      data: data
    })
      .then(res => {
        this.prevent = true
        // if (res.data.code != 0) return mui.toast(res.data.msg)
        callback(res.data)
      })
      .catch(err => {
        this.prevent = true
        console.log('axios post错误:', err)
      })
  },
  /**
   * 统一get请求
   * @param {string} url请求路径 请求路径
   * @param {*} params 数据
   * @param {funciton} callback 成功回调
   */
  $get(url, callback, params) {
    this.prevent = true
    if (!this.prevent) {
      return
    }
    this.prevent = false
    axios
      .get(url, {
        params
      })
      .then(res => {
        this.prevent = true

        callback(res.data)

        // mui.toast(res.data.msg)
        // if (res.data.code != 0) return
      })
      .catch(err => {
        this.prevent = true
        console.log('axios get错误:', err)
      })
  },

  /**
   * 统一cookie操作
   * @param {string} request 操作方式,get,set,del
   * @param {string} name cookie名
   * @param {*} value 值
   * @param {number} time cookie保留的时间
   */
  cookie(request, name, value, time) {
    //   获取cookie,
    if (request == 'get') {
      var arr,
        reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
      if ((arr = document.cookie.match(reg))) {
        return JSON.parse(unescape(arr[2]))
      } else return null
    }
    // 设置cookie
    if (request == 'set') {
      var strsec = getsec(time)
      var exp = new Date()
      exp.setTime(exp.getTime() + strsec * 1)
      document.cookie =
        name + '=' + escape(value) + ';expires=' + exp.toGMTString()
    }
    // 删除cookie
    if (request == 'del') {
      var exp = new Date()
      exp.setTime(exp.getTime() - 1)
      var arr,
        reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)')
      if ((arr = document.cookie.match(reg))) {
        var cval = unescape(arr[2])
        if (cval != null)
          document.cookie = name + '=' + cval + ';expires=' + exp.toGMTString()
      }
    }
    // 计算cookie时间
    //s20是代表20秒
    //h是指小时，如12小时则是：h12
    //d是天数，30天则：d30
    function getsec(str) {
      var str1 = str.substring(1, str.length) * 1
      var str2 = str.substring(0, 1)
      if (str2 == 's') {
        return str1 * 1000
      } else if (str2 == 'h') {
        return str1 * 60 * 60 * 1000
      } else if (str2 == 'd') {
        return str1 * 24 * 60 * 60 * 1000
      }
    }
  },
  // 时间戳处理
  parseDate(time) {
    var delta = Date.now() - time
    var d = parseInt(delta / 24 / 60 / 60 / 1000)
    var h = parseInt(delta / 60 / 60 / 1000)
    var m = parseInt(delta / 60 / 1000)

    if (d > 0 && d < 4) {
      return d + '天前'
    } else if (d > 3) {
      return new Date(time).Format('MM-DD HH:mm')
    } else if (h > 0) {
      return h + '小时前'
    } else if (m > 0) {
      return m + '分钟前'
    } else {
      return '刚刚'
    }
  },
  /**
   * 上传进度时间格式化
   * @param {number} time
   */
  progressTime(time) {
    let h = parseInt(time / 60 / 60)
    let m = Math.abs(h - parseInt(time / 60))
    let s = time
    if (String(h).length >= 2) {
      h = '0' + h
    }
    if (String(m).length >= 2) {
      m = '0' + m
    }
    if (String(s).length >= 2) {
      s = '0' + s
    }
    return h + ':' + m + ':' + s
  },

  // 浏览器判断
  browser: (function() {
    var u = navigator.userAgent.toLowerCase()
    return {
      android: u.indexOf('android') > -1 || u.indexOf('linux') > -1, //android
      ios: u.indexOf('iphone') > -1 || u.indexOf('ipad') > -1, //iPhone
      weixin: u.indexOf('micromessenger') > -1 // 微信
    }
  })(),

  /**
   * 不同的路由显示不同的文字,
   * @param {string} router 导航路径
   */
  routerMenu(router) {
    switch (router) {
      case 'video_only':
        return '单一视频'
      case 'video_seriec':
        return '系列视频'
      case 'only_up':
        return '上传单一视频'
      case 'seriec_up':
        return '上传系列视频'
      case 'changePassword':
        return '修改密码'
      case 'changePhoneNext':
        return '更换手机号'
      case 'changePhone':
        return '更换手机号'
      case 'all_video':
        return '上传系列视频'
      case 'professionalApprove':
        return '专业认证'
      case 'degreeApprove':
        return '学位认证'
      case 'teacherApprove':
        return '教师认证'
      case 'studentApprove':
        return '学生认证'
      case 'educationApprove':
        return '学历认证';
      case 'identityApprove':
        return '身份认证';  
      case 'dataEdit':
        return '资料编辑';  
    }
  }
}

/**
 *
 * 格式化时间
 * @param  {String} formatStr yyyy-mm-dd hh:mm:ss w
 * @return {String}   时间字符串 "2017-06-02 14:05:01 六"
 *
 */
Date.prototype.Format = function (formatStr) {
  var str = formatStr
  var Week = ['日', '一', '二', '三', '四', '五', '六']
  str = str.replace(/yyyy|YYYY/, this.getFullYear())
  str = str.replace(
    /yy|YY/,
    this.getYear() % 100 > 9 ?
    (this.getYear() % 100).toString() :
    '0' + this.getYear() % 100
  )
  str = str.replace(
    /MM/,
    this.getMonth() + 1 > 9 ?
    (this.getMonth() + 1).toString() :
    '0' + (this.getMonth() + 1)
  )
  str = str.replace(/M/g, this.getMonth() + 1)
  str = str.replace(/w|W/g, Week[this.getDay()])
  str = str.replace(
    /dd|DD/,
    this.getDate() > 9 ? this.getDate().toString() : '0' + this.getDate()
  )
  str = str.replace(/d|D/g, this.getDate())
  str = str.replace(
    /hh|HH/,
    this.getHours() > 9 ? this.getHours().toString() : '0' + this.getHours()
  )
  str = str.replace(/h|H/g, this.getHours())
  str = str.replace(
    /mm/,
    this.getMinutes() > 9 ?
    this.getMinutes().toString() :
    '0' + this.getMinutes()
  )
  str = str.replace(/m/g, this.getMinutes())
  str = str.replace(
    /ss|SS/,
    this.getSeconds() > 9 ?
    this.getSeconds().toString() :
    '0' + this.getSeconds()
  )
  str = str.replace(/s|S/g, this.getSeconds())
  return str
}

export default utils
